package aiku.numericalcontroloa.Task;

import aiku.numericalcontroloa.Mapper.DevicesMapper;
import aiku.numericalcontroloa.Mapper.ProductPartMapper;
import aiku.numericalcontroloa.Model.Entity.Devices;
import aiku.numericalcontroloa.Model.Entity.ProductPart;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 普通任务
 * @Author HuangGuangTing
 * @Date 2024/8/20 18:27
 */
@Component
public class NormalTask {
    @Autowired
    private DevicesMapper devicesMapper;
    @Autowired
    private ProductPartMapper partMapper;

    /**
     * 根据零件列表，更新所组成成品的成本价
     * @param partIdList
     */
    @Transactional
    public void updateProductPrice(Set<String> partIdList){
        // 根据零件，查询零件列表
        LambdaQueryWrapper<ProductPart> partLQW = new LambdaQueryWrapper<>();
        partLQW.in(ProductPart::getPartId,partIdList);
        List<ProductPart> productPartList = partMapper.selectList(partLQW);
        if (productPartList.isEmpty()){
            System.out.println("没有组成成品");
            return;
        }
        // 获取成品id
        Set<String> devicesIdList = productPartList.stream().map(ProductPart::getDevicesId).collect(Collectors.toSet());
        // 通过成品id列表的所有零件列表
        partLQW = new LambdaQueryWrapper<>();
        partLQW.in(ProductPart::getDevicesId,devicesIdList);
        List<ProductPart> partList = partMapper.selectList(partLQW);
        // 根据partIds查询产品表
        Set<String> partIds = partList.stream().map(ProductPart::getPartId).collect(Collectors.toSet());
        partIds.addAll(devicesIdList);
        List<Devices> devicesList = devicesMapper.selectBatchByIdWithDelete(partIds);
        // 通过devicesId获取devices
        Map<String, Devices> devicesMap = devicesList.stream().collect(Collectors.toMap(Devices::getId, Function.identity()));
        // 通过成品id，获取零件列表
        Map<String,List<ProductPart>> partMap = new HashMap<>();
        for (ProductPart productPart : partList){
            if (partMap.containsKey(productPart.getDevicesId())){
                partMap.get(productPart.getDevicesId()).add(productPart);
            }else {
                List<ProductPart> parts = new ArrayList<>();
                parts.add(productPart);
                partMap.put(productPart.getDevicesId(),parts);
            }
        }
        // 计算
        List<Devices> updateList = new ArrayList<>();
        for (String devicesId : devicesIdList){
            if (!partMap.containsKey(devicesId)){
                continue;
            }
            Devices devices = devicesMap.get(devicesId);
            List<ProductPart> parts = partMap.get(devices.getId());
            // 重新计算成品成本价
            BigDecimal cost = new BigDecimal(0);
            for (ProductPart productPart : parts){
                Devices part1 = devicesMap.get(productPart.getPartId());
                BigDecimal multiply = part1.getCost().multiply(new BigDecimal(productPart.getNumber()));
                cost = cost.add(multiply);
            }
            devices.setCost(cost);
            updateList.add(devices);
        }
        // 更新成品
        if (!updateList.isEmpty()){
            devicesMapper.updateCountBatchById(updateList);
        }
        System.out.println("更新成功");
    }
}
